<?php

// +----------------------------------------------------------------------
// | ThinkAdmin
// +----------------------------------------------------------------------
// | 版权所有 2014~2017 广州楚才信息科技有限公司 [ http://www.cuci.cc ]
// +----------------------------------------------------------------------
// | 官方网站: http://think.ctolog.com
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zoujingli/ThinkAdmin
// +----------------------------------------------------------------------

namespace app\wechat\controller;

use app\wechat\service\FansService;
use app\wechat\service\TagsService;
use controller\BasicAdmin;
use service\DataService;
use service\LogService;
use service\ToolsService;
use service\WechatService;
use think\Db;
use think\Validate;

/**
 * 微信粉丝管理
 * Class Fans
 * @package app\wechat\controller
 * @author Anyon <zoujingli@qq.com>
 * @date 2017/03/27 14:43
 */
class Full extends BasicAdmin
{

    /**
     * 定义当前默认数据表
     * @var string
     */
    public $table = 'store_apply_record';

    /**
     * 显示粉丝列表
     * @return array|string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\Exception
     */
    public function index()
    {
        $this->title = '全价保养记录';
        $get = $this->request->get();

        $db = Db::table('store_apply_record a')
            ->field('a.*,b.phone,b.nickname')
            ->join('store_member b',"a.uid = b.id")
            ->where(['type'=>'is_full_price']);

        if($this->user['id'] != 10000) {
            if (strlen($this->shopUnderUsers) > 0 || empty($this->shopUnderUsers)) {
                $db->where('b.id', 'in', $this->shopUnderUsers);
            }
        }
        if (isset($get['create_at']) && $get['create_at'] !== '') {
            list($start, $end) = explode(' - ', $get['create_at']);
            $db->whereBetween('a.addtime', ["{$start} 00:00:00", "{$end} 23:59:59"]);
        }

        if (isset($get['nickname']) && $get['nickname'] !== '') {
            $db->where('b.nickname', 'like',"%{$get['nickname']}%");
        }

        if (isset($get['phone']) && $get['phone'] !== '') {
            $db->where('b.phone', 'like',"%{$get['phone']}%");
        }

        return parent::_list($db->order('a.addtime desc'));
    }

    public function _index_data_filter( &$data )
    {
    }

    public function useradd()
    {
        return $this->_form($this->table, 'user_add');
    }
    public function useredit()
    {
        return $this->_form($this->table, 'user_add');
    }


    //执行文章状态的操作
    public function articlestatus()
    {
        if (request()->isPost()) {
            $data = request()->post();
            $id = $data['id'];
            if (empty($id))
                $this->error('你好,请选择要操作的数据');

            $where['id'] = array('in', $id);
            $method = $data['field'];
            switch (strtolower($method)) {
                case 'resume':
                    $data = array('status' => 0);
                    break;
                case  'forbid':
                    $data = array('status' => 1);
                    break;
                case  'delete':
                    $res = Db::name($this->table)->where('id','in',$id)->delete();
                    if ($res) {
                        $this->success('删除成功', '');
                    } else {
                        $this->error('删除失败');
                    }
                    break;
                default:
                    $this->error('参数非法');
            }
            if (Db::name('es_article')->where($where)->update($data)) {
                $this->success('操作成功', '');
            } else {
                $this->error('操作失败');
            }
        }
    }

    public function user_balance()
    {

        return view();
    }


    public function backadd(){
        $data = input('post.');
        $validate = Validate::make([
            'id' => 'require',
            'worker_id' => 'require',
        ], [
            'id.require' => 'id不能为空！',
            'worker_id.require' => 'iworker_id不能为空！',
        ]);
        $validate->check($data) || $this->error($validate->getError());

        $flag = Db::name('store_apply_record')->where(['id'=>$data['id'],'status'=>1])->find();
        if(!$flag) $this->error('已经被处理或者被删除');
        Db::startTrans();
        $res[] = DataService::other_save('store_apply_record',[$data['field']=>$data['value'],'worker_id'=>$data['worker_id']],['id'=>$data['id']]);
        if($data['value'] == 2){
            $memberInfo = Db::name('store_member')->where(['id'=>$flag['uid']])->find();
            $flag_shares = Db::name('bags_log')->where(['fromid'=>$memberInfo['id'],'uid'=>$memberInfo['first_leader']])->find();
            if(!$flag_shares){
                $res['upgrade_user'] = DataService::other_save('store_member',['member_level'=>2],['id'=>$flag['uid']]);
                if($memberInfo['first_leader'] > 0){
                    $res['giveshare'] = mlog($memberInfo['first_leader'],'spread_integral',sysconf('share_spread_integral'),"您的团队{$memberInfo['nickname']}升级成为VIP",'share_spread_integral','','',$memberInfo['id']);


                }
            }
            $res[] = mlog($flag['uid'],'integral',$flag['num'],'申请全价保养赠送积分:'.$flag['num'],'give_integral');
        }
        $res[] = DataService::other_save('store_member',['is_full_price'=>1],['id'=>$flag['uid']]);
        if(check_arr($res)){
            Db::commit();
           $this->success('操作成功','');
        }else{
            Db::rollback();
            $this->error('网络故障,操作失败');
        }
    }
}
